package org.pcorp.battlespace.construction.ui.admin.arm;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.pcorp.battlespace.modele.Capacite;
import org.pcorp.battlespace.modele.equipement.Armement;

public class ModelTableArmAdmin extends AbstractTableModel {
	private static final long serialVersionUID = -8890518024105484051L;
	private List<Armement> armements;
	private List<String> entetes;

	public static final int NOM=0;
	public static final int X=1;
	public static final int Y=2;
	public static final int TYPE=3;
	public static final int STYPE=4;
	public static final int PORT=5;
	public static final int MASS=6;
	public static final int OP=7;
	public static final int ENC=8;
	public static final int LIM=9;
	public static final int DEC=10;
	public static final int CAP=11;
	public static final int SUP=12;
	public static final int SIZE=13;

	public ModelTableArmAdmin(List<Armement> armements) {
		super();
		this.armements = armements;
		init();
	}

	public ModelTableArmAdmin() {
		super();
		init();
	}

	private void init() {
		entetes = new ArrayList<String>();
		int i=0;
		boolean encore=true;
		
		while (encore) {
			switch (i) {
				case NOM:
					entetes.add("Nom");
					break;
				case X:
					entetes.add("X");
					break;
				case Y:
					entetes.add("Y");
					break;
				case TYPE:
					entetes.add("T");
					break;
				case STYPE:
					entetes.add("ST");
					break;
				case PORT:
					entetes.add("P");
					break;
				case MASS:
					entetes.add("M");
					break;
				case OP:
					entetes.add("OP");
					break;
				case ENC:
					entetes.add("Enc");
					break;
				case LIM:
					entetes.add("Lim");
					break;
				case DEC:
					entetes.add("Desc");
					break;
				case CAP:
					entetes.add("C");
					break;
				case SUP:
					entetes.add("Supr");
					break;
				default:
					encore=false;
					break;
			}
			i++;
		}
	}
	
	@Override
	public int getRowCount() {

		if (armements==null) 
			return 0;
		return armements.size();
	}

	@Override
	public int getColumnCount() {

		return entetes.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Armement arm = armements.get(rowIndex);

		switch (columnIndex) {
			case NOM:
				return arm.getNom();
			case X:
				return arm.getDegatX();
			case Y:
				return arm.getDegatY();
			case TYPE:
				return arm.getType();
			case STYPE:
				return arm.getSsType();
			case PORT:
				return arm.getPortee();
			case DEC:
				return arm.getDescription();
			case CAP:
				return getCapa(arm);
			case MASS:
				return arm.getMasse();
			case OP:
				return arm.getOperateurs();
			case ENC:
				return arm.getEncombrement();
			case LIM:
				return getRestriction(arm);
			case SUP:
				return false;
			default:
				return "erreur";
		}
	}
	private String getRestriction(Armement arm) {
		String l = "";
		for (String s :  arm.getRestrictions()) {
			if (l != "")
				l += ", ";
			if (s.equalsIgnoreCase("CHASSEUR"))
				l += "CHA";
			if (s.equalsIgnoreCase("CABOTEUR"))
				l += "CAB";
			if (s.equalsIgnoreCase("STATION"))
				l += "STA";
			if (s.equalsIgnoreCase("FRAPPEUR"))
				l += "FRA";
			if (s.equalsIgnoreCase("SAUTEUR"))
				l += "SAT";
		}
		return l;
	}
	
	private String getCapa(Armement arm) {
		String l=null;
		for (Capacite cap : arm.getCapacites()) {
			if (l==null) {
				// TODO: label
				l=cap.getCode();
			}
			else {
				l+=", " + cap.getCode();
			}
				
		}
		return l;
	}
	
	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		Armement arm = armements.get(rowIndex);
		switch (columnIndex) {
			case NOM:
				arm.setNom((String)aValue);
				break;
			case X:
				arm.setDegatX(Long.parseLong((String)aValue));
				break;
			case Y:
				arm.setDegatY(Long.parseLong((String)aValue));
				break;
			case TYPE:
				arm.setType((String)aValue);
				break;
			case STYPE:
				arm.setSsType((String)aValue);
				break;
			case PORT:
				arm.setPortee(Long.parseLong((String)aValue));
				break;
			case DEC:
				arm.setDescription((String)aValue);
				break;
			case CAP:
				//			arm.setCapacites(aValue); //TODO: affichage code seulement
				break;
			case MASS:
				arm.setMasse(Long.parseLong((String)aValue));
				break;
			case OP:
				arm.setOperateurs(Long.parseLong((String)aValue));
				break;
			case ENC:
				arm.setEncombrement(Long.parseLong((String)aValue));
				break;
			case LIM:
				//arm.setRestrictions(aValue);
				break;
		}
		arm.setUpdate(true); // permet de lancer la sauvegarde
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		switch (columnIndex) {
			case CAP:
				return false;
			case LIM:
				return false;
			default:
				return true;
		}
	}

	@Override
	public String getColumnName(int column) {

		return entetes.get(column);
	}

	public Armement getRow(int row) {
		return armements.get(row);
	}
	
	public List<Armement> getArmements() {
		return armements;
	}

	public void setArmements(List<Armement> armements) {
		this.armements = armements;
	}

}
